import random
import time


from selenium import webdriver
from selenium.webdriver import ActionChains


driver = webdriver.Chrome()
driver.get("https://www.moguoyunke.com")
# 将浏览器窗口最大化
driver.maximize_window()
time.sleep(1)
# 登录
driver.find_element("xpath", '//button[text()="登录"]').click()  # 点击登录
time.sleep(2)
driver.find_element("xpath", '//*[text()=" 账号密码 "]').click()  # 点击账号密码按钮
time.sleep(1)
# 定位登录账号文本框
ele_user = driver.find_element("xpath", '//input[@tabindex="1"]')
time.sleep(1)
ele_user.click()  # 点击“账号”文本框
ele_user.send_keys("15732069727")  # 输入登录账号
time.sleep(1)
# 定位登录密码文本框
ele_user = driver.find_element("xpath", '//input[@tabindex="2"]')
time.sleep(1)
ele_user.click()  # 点击“密码”文本框
time.sleep(1)
ele_user.send_keys("Zhixin@2025")  # 输入登录密码
time.sleep(1)
# 勾选“我已阅读并同意服务协议和隐私政策”复选框
driver.find_element("xpath", '//div/p[@class="protocol-box"][2]/label').click()
# 点击登录按钮
time.sleep(1)
driver.find_element("xpath", '//span[text()=" 登录 "]').click()
time.sleep(0.3)
msg = driver.find_element("xpath", '//p[@class="el-message__content"]').text
print(msg)
assert "登录成功" == msg
time.sleep(1)
# 点击学习中心
driver.get("https://www.moguoyunke.com/my-course")
time.sleep(1)
course_ele=driver.find_element("xpath", '//li[@class="my-course-crad"][9]')
driver.execute_script("arguments[0].scrollIntoView();", course_ele)
course_ele.click()
time.sleep(4)
# 点击课时名称为：6月3日早会
driver.find_element("xpath",'//span[text()="6月3日早会"]').click()
time.sleep(6)
# 点击播放按钮
driver.find_element("xpath",'//div[@class="play-pause-btn"]').click()
# 点击倍速按钮
driver.find_element("xpath",'//span[@class="speed-btn"]').click()
time.sleep(0.5)
# 切换1.5倍速
driver.find_element("xpath",'//span[text()=" 1.5X "]').click()
time.sleep(1)
# 点击前进5s按钮 快进 5 秒
driver.find_element("xpath",'//div[@class="forward-btn"]').click()
time.sleep(0.5)
msg=driver.find_element("xpath",'//div[@class="notice-box"]').text
print("点击快进",msg)
assert msg=='快进 5 秒'
time.sleep(2)
# 点击后台5s
driver.find_element("xpath",'//div[@class="back-btn"]').click()
msg=driver.find_element("xpath",'//div[@class="notice-box"]').text
assert msg=='快退 5 秒'
time.sleep(1)
# 点击全屏按钮
driver.find_element("xpath",'//div[@class="full-screen-btn"]').click()
time.sleep(1)
# 点击退出全屏按钮
driver.find_element("xpath",'//div[@class="full-screen-btn"]').click()
time.sleep(1)
# 点击截图
driver.find_element("xpath",'//div[@class="operate-item"]').click()
time.sleep(0.5)
# 判断截图是否成功
msg=driver.find_element("xpath",'//div[@class="moguo-toast show"]').text
time.sleep(0.2)
assert msg=="截图成功"
time.sleep(1)
# 点击标记按钮
driver.find_element("xpath",'//button[@class="mark-btn"]').click()
time.sleep(0.3)
#定义一个列表存储标记的选项
mark_list=[]
popo_ele_list=driver.find_elements("xpath",'//span[@class="item-title"]')
if popo_ele_list:
    for ele in popo_ele_list:

        print(f"当前元素为：{ele.text}")
        mark_list.append(ele.text)
for ele_text in mark_list:

        # ele.click()
    driver.find_element("xpath",f'//span[text()="{ele_text}"]').click()
    time.sleep(0.3)

    # # print(f"点击{ele.text}")
    # time.sleep(0.3)
    if ele_text == "自定义":
        # 输入笔记
        driver.find_element("xpath", '//div[@class="mark-title-input el-input"]/input').send_keys(f"我是袁世凯他奶奶{random.randint(1, 1000)}")
        time.sleep(0.3)
        # 点击保存按钮
        driver.find_element("xpath", '//button[text()=" 保存 "]').click()
    time.sleep(0.5)
    # 判断重点标记是否成功
    msg = driver.find_element("xpath", '//div[@class="moguo-toast show"]').text
    time.sleep(0.5)
    assert msg == "标记成功"
    time.sleep(2)

    driver.find_element("xpath", '//button[@class="mark-btn"]').click()
    time.sleep(1)
# 拉取视频进度条
progress_bar=driver.find_element("xpath", '//div[@class="draggable-progress-bar"]')
current_length=progress_bar.location["x"]
totoal_length=progress_bar.size["width"]
# 定义拖动到的位置，例如拖动到总长度的90%位置
target_x=current_length+totoal_length*0.9
# 使用ActionChains类进行拖动
# 创建ActionChains对象
action=ActionChains(driver)
action.click_and_hold(progress_bar).move_by_offset(target_x-current_length,0).release().perform()
driver.find_element("xpath", '//*[@id="app"]/div[1]/div/div/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]').click()
time.sleep(0.3)
msg = driver.find_element("xpath", '//div[@class="tips"]/p').text
assert msg == "保存成功"

# 点击导出按钮
driver.find_element("xpath", '//*[@id="app"]/div[1]/div/div/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]').click()
time.sleep(1)
print("当前网页地址为：",driver.current_url)
assert driver.current_url=="https://www.moguoyunke.com/video-play/630763293745221/911/3450"
time.sleep(0.3)
# 获取所有窗口的句柄
all_windows=driver.window_handles
print("获取所有窗口的句柄", all_windows)
# 切换到当前最新打开的窗口

driver.switch_to.window(all_windows[-1])
print("当前窗口的句柄", driver.current_window_handle)
# 点击下载按钮
driver.find_element("xpath", '//div[@class="note-title"]/button').click()
time.sleep(0.5)
msg = driver.find_element("xpath", '//p[@class="el-loading-text"]').text
assert msg == "PDF生成中"

time.sleep(0.3)
